package com.boss.salary.dao.impl;

import com.boss.common.util.DateUtils;
import com.boss.framework.dao.BaseDaoImpl;
import com.boss.salary.constant.TableName;
import com.boss.salary.dao.ISalaryMonthBeginDao;
import com.boss.salary.entity.SalaryMonthEnd;
import com.boss.salary.vo.SalaryMonthEndVO;
import com.boss.sys.utils.SysUtils;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Repository
public class SalaryMonthBeginDaoImpl extends BaseDaoImpl implements ISalaryMonthBeginDao {

    @Override
    public void batchSaveSalaryMonthBegin(List<SalaryMonthEndVO> salaryMonthEndVOList) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        List<Object[]> paramList = new ArrayList<>(salaryMonthEndVOList.size());
        Class<?> clazz = SalaryMonthEndVO.class;
        for(SalaryMonthEndVO salaryMonthEndVO : salaryMonthEndVOList){
            Object[] paramArr = {
                    SysUtils.generateGuid(), String.valueOf(DateUtils.getCurrentTimeStamp()), String.valueOf(DateUtils.getCurrentTimeStamp()),
                    salaryMonthEndVO.getAgencyId(), salaryMonthEndVO.getPersonId(),salaryMonthEndVO.getIsThirteen(), salaryMonthEndVO.getMofDivCode()
            };
            List<Object> params = new ArrayList<>(Arrays.asList(paramArr));
            for(int i = 1; i <=150; i++){
                Method getMethod = clazz.getDeclaredMethod("getS"+i);
                params.add(getMethod.invoke(salaryMonthEndVO));
            }
            paramList.add(params.toArray());
        }
        StringBuilder sb = new StringBuilder();
        sb.append(" insert into ").append(TableName.SALARY_MONTH_BEGIN)
                .append(" ( id, create_time, update_time, agency_id, person_id,is_thirteen,mof_div_code, ")
                .append(" s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19, s20,  ")
                .append(" s21, s22, s23, s24, s25, s26, s27, s28, s29, s30, s31, s32, s33, s34, s35, s36, s37, s38, s39, s40, ")
                .append(" s41, s42, s43, s44, s45, s46, s47, s48, s49, s50, s51, s52, s53, s54, s55, s56, s57, s58, s59, s60, ")
                .append(" s61, s62, s63, s64, s65, s66, s67, s68, s69, s70, s71, s72, s73, s74, s75, s76, s77, s78, s79, s80, ")
                .append(" s81, s82, s83, s84, s85, s86, s87, s88, s89, s90, s91, s92, s93, s94, s95, s96, s97, s98, s99, s100, ")
                .append(" s101,s102,s103,s104,s105,s106,s107,s108,s109,s110,s111,s112,s113,s114,s115,s116,s117,s118,s119,s120, ")
                .append(" s121,s122,s123,s124,s125,s126,s127,s128,s129,s130,s131,s132,s133,s134,s135,s136,s137,s138,s139,s140, ")
                .append(" s141,s142,s143,s144,s145,s146,s147,s148,s149,s150 )")
                .append(" values ( ?, ?, ?, ?, ?, ?, ?, ")
                .append(" ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ")
                .append(" ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ")
                .append(" ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ")
                .append(" ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ")
                .append(" ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ")
                .append(" ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ")
                .append(" ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ")
                .append(" ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) ");
        super.getJdbcTemplate().batchUpdate(sb.toString(), new BatchPreparedStatementSetter() {
            public int getBatchSize() {
                return paramList.size();
                //这个方法设定更新记录数，通常List里面存放的都是我们要更新的，所以返回list.size();
            }
            public void setValues(PreparedStatement ps, int i)throws SQLException {
                Object[] sme = paramList.get(i);
                for(int a = 0; a < 157; a++){
                    ps.setObject(a+1,sme[a]);
                }

            }
        });
    }

    @Override
    public void batchPhysicalDeleteSalaryMonthBegin(String province, List<String> agencyIds) {
        StringBuilder sb = new StringBuilder();
        sb.append(" delete from ").append(TableName.SALARY_MONTH_BEGIN)
                .append(" where ").append(" mof_div_code = ? ");
        if(!CollectionUtils.isEmpty(agencyIds)){
            sb.append(" and ").append(SysUtils.inSql("agency_id", agencyIds));
        }
        super.getjdbcTemplate().update(sb.toString(), province);
    }
    @Override
    public SalaryMonthEnd selectSalaryBeginByPersonId(String personId) {
        StringBuilder sb = new StringBuilder();
        sb.append(" select ")
                .append(" id, create_time, update_time, agency_id, person_id,mof_div_code,")
                .append(" s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19, s20, ")
                .append(" s21, s22, s23, s24, s25, s26, s27, s28, s29, s30, s31, s32, s33, s34, s35, s36, s37, s38, ")
                .append(" s39, s40, s41, s42, s43, s44, s45, s46, s47, s48, s49, s50, s51, s52, s53, s54, s55, s56, ")
                .append(" s57, s58, s59, s60, s61, s62, s63, s64, s65, s66, s67, s68, s69, s70, s71, s72, s73, s74, ")
                .append(" s75, s76, s77, s78, s79, s80, s81, s82, s83, s84, s85, s86, s87, s88, s89, s90, s91, s92, ")
                .append(" s93, s94, s95, s96, s97, s98, s99, s100, ")
                .append(" s101,s102,s103,s104,s105,s106,s107,s108,s109,s110,s111,s112,s113,s114,s115,s116,s117,s118,s119,s120, ")
                .append(" s121,s122,s123,s124,s125,s126,s127,s128,s129,s130,s131,s132,s133,s134,s135,s136,s137,s138,s139,s140, ")
                .append(" s141,s142,s143,s144,s145,s146,s147,s148,s149,s150 ")
                .append(" from ")
                .append(TableName.SALARY_MONTH_BEGIN)
                .append(" where ").append(" person_id = ?");
        Object[] params = {
                personId
        };
        try {
            return super.getJdbcTemplate().queryForObject(sb.toString(), params, new BeanPropertyRowMapper<>(SalaryMonthEnd.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }

    }

    @Override
    public List<SalaryMonthEndVO> selectSalaryMonthEndWithSalaryType(String province, List<String> agencyIds) {
        StringBuilder sb = new StringBuilder();
        List<Object> paramList = new ArrayList<>();
        sb.append(" select ")
                .append(" pet.agency_id, pet.person_id, pet.merit_pay_code, pet.mof_div_code,is_thirteen, ")
                .append(" s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19, s20, ")
                .append(" s21, s22, s23, s24, s25, s26, s27, s28, s29, s30, s31, s32, s33, s34, s35, s36, s37, s38, s39, s40, ")
                .append(" s41, s42, s43, s44, s45, s46, s47, s48, s49, s50, s51, s52, s53, s54, s55, s56, s57, s58, s59, s60, ")
                .append(" s61, s62, s63, s64, s65, s66, s67, s68, s69, s70, s71, s72, s73, s74, s75, s76, s77, s78, s79, s80, ")
                .append(" s81, s82, s83, s84, s85, s86, s87, s88, s89, s90, s91, s92, s93, s94, s95, s96, s97, s98, s99, s100, ")
                .append(" s101,s102,s103,s104,s105,s106,s107,s108,s109,s110,s111,s112,s113,s114,s115,s116,s117,s118,s119,s120, ")
                .append(" s121,s122,s123,s124,s125,s126,s127,s128,s129,s130,s131,s132,s133,s134,s135,s136,s137,s138,s139,s140, ")
                .append(" s141,s142,s143,s144,s145,s146,s147,s148,s149,s150 ")
                .append(" from ").append(TableName.SAL_PERSON_EXT_BEGIN).append(" pet ")
                .append(" join ").append(TableName.SALARY_MONTH_BEGIN).append(" smed ")
                .append(" on ").append(" pet.person_id = smed.person_id ")
                .append(" where ").append(" pet.is_deleted = 0 and smed.mof_div_code = ? ");
        paramList.add(province);

            sb.append(" and ").append(SysUtils.inSql("smed.agency_id", agencyIds));

        try{
            return super.getJdbcTemplate().query(sb.toString(), paramList.toArray(), new BeanPropertyRowMapper<>(SalaryMonthEndVO.class));
        }catch(EmptyResultDataAccessException e){
            return Collections.emptyList();
        }
    }
}
